# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=zlib
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2.11
pkgrel=1
pkgdesc="Compression library implementing the deflate compression method found in gzip and PKZIP (mingw-w64)"
arch=('any')
license=(ZLIB)
url="https://www.zlib.net/"
depends=("${MINGW_PACKAGE_PREFIX}-bzip2")
makedepends=('git')
options=('staticlibs')
source=("https://zlib.net/current/${_realname}-${pkgver}.tar.gz"
        "git+https://github.com/nmoinvaz/minizip.git#commit=7914ff3c"
        01-zlib-1.2.11-1-buildsys.mingw.patch
        03-dont-put-sodir-into-L.mingw.patch
        04-fix-largefile-support.patch
        010-unzip-add-function-unzOpenBuffer.patch
        011-Add-no-undefined-to-link-to-enable-build-shared-vers.patch
        012-Add-bzip2-library-to-pkg-config-file.patch)
sha256sums=('c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1'
            'SKIP'
            'ac91f905b695d71f6c9c471ac98c14a3ed989a1e2b2a3b1171b3f6dc6bfc31b4'
            '3d039f42194aade91dadf4174f55fd6db349fd8044db93875bed1042dcfe1f31'
            '3b36fe536a7458af2a9a494d70d24048da10c43423fd620ed93fa0a6ddd14f78'
            'e3e85ac0df92d4b0a777631c064ae92c9a73d9f8baea93d35e01620838c4010d'
            'ebf118d3754c1cb9a07f391bfdb24d2941bc4e11a7b3956b17fa095f97e6eae8'
            '08269402dc15bb6d1600e011633ca3ace43e2d74613fdabdd516a05f5939cc78')

prepare() {
  # Replace minizip with external
  rm -rf ${srcdir}/${_realname}-${pkgver}/contrib/minizip
  cp -r ${srcdir}/minizip/ ${srcdir}/${_realname}-${pkgver}/contrib

  cd ${srcdir}/${_realname}-${pkgver}
  grep -A 24 '^  Copyright' zlib.h > LICENSE

  patch -p1 -i ${srcdir}/01-zlib-1.2.11-1-buildsys.mingw.patch
  patch -p2 -i ${srcdir}/03-dont-put-sodir-into-L.mingw.patch
  patch -p1 -i ${srcdir}/04-fix-largefile-support.patch

  cd ${srcdir}/${_realname}-${pkgver}/contrib/minizip
  git am ${srcdir}/010-unzip-add-function-unzOpenBuffer.patch
  git am ${srcdir}/011-Add-no-undefined-to-link-to-enable-build-shared-vers.patch
  git am ${srcdir}/012-Add-bzip2-library-to-pkg-config-file.patch

}

build() {
  cd ${srcdir}/${_realname}-${pkgver}
  CHOST=${MINGW_CHOST} ./configure --prefix=${MINGW_PREFIX} --shared
  make -j1 all

  # build minizip
  pushd contrib/minizip > /dev/null
  autoreconf -fi
  CFLAGS+=" -DHAVE_BZIP2"
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-demos \
    LIBS="-lbz2"
  make
  popd > /dev/null
}

check() {
  cd ${srcdir}/${_realname}-${pkgver}
  make test
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}
  make install DESTDIR="${pkgdir}"
  pushd contrib/minizip > /dev/null
  make install DESTDIR="${pkgdir}"
  popd > /dev/null
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
